package gui;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.ProduktType;
/**
 * 
 * @author Søren
 *
 */
public class RedigerProduktTyper extends JPanel implements CarlettiPanel {
	private JScrollPane scrollPane;
	private JList list;
	private final JPanel panel = new JPanel();
	private JLabel lblNavn;
	private JTextField textField;
	private JLabel lblBeskrivelse;
	private JTextField textField_1;
	private JLabel lblBehandlinger;
	private JButton btnSletProdukttype;
	private JButton btnOpretProdukttype;
	private JButton btnRedigerProdukttype;
	private MainFrame m;
	private JTextField textField_2;
	private JPanel panel_1;

	public RedigerProduktTyper(MainFrame m) {

		setLayout(new GridLayout(1, 0, 0, 0));
		scrollPane = new JScrollPane();
		add(scrollPane);

		this.m = m;

		list = new JList();
		list.addListSelectionListener(new ListListSelectionListener());
		list.setVisibleRowCount(20);
		scrollPane.setViewportView(list);
		add(panel);
		panel.setLayout(new GridLayout(0, 2, 0, 0));

		lblNavn = new JLabel("Navn");
		panel.add(lblNavn);

		textField = new JTextField();
		textField.setFont(new Font("Dialog", Font.PLAIN, 16));
		textField.setEditable(false);
		panel.add(textField);
		textField.setColumns(10);

		lblBeskrivelse = new JLabel("Beskrivelse");
		panel.add(lblBeskrivelse);

		textField_1 = new JTextField();
		textField_1.setFont(new Font("Dialog", Font.PLAIN, 16));
		textField_1.setEditable(false);
		panel.add(textField_1);
		textField_1.setColumns(10);

		lblBehandlinger = new JLabel("Behandling");
		panel.add(lblBehandlinger);

		btnSletProdukttype = new JButton("Slet produkttype");
		btnSletProdukttype
				.addActionListener(new BtnSletProdukttypeActionListener());

		textField_2 = new JTextField();
		textField_2.setFont(new Font("Dialog", Font.PLAIN, 16));
		textField_2.setEditable(false);
		panel.add(textField_2);
		textField_2.setColumns(10);
		panel.add(btnSletProdukttype);

		btnOpretProdukttype = new JButton("Opret produkttype");
		btnOpretProdukttype
				.addActionListener(new BtnOpretProdukttypeActionListener());
		panel.add(btnOpretProdukttype);

		btnRedigerProdukttype = new JButton("Rediger valgt proukttype");
		btnRedigerProdukttype
				.addActionListener(new BtnRedigerProdukttypeActionListener());

		panel_1 = new JPanel();
		panel.add(panel_1);
		panel.add(btnRedigerProdukttype);

		list.setListData(service.Service.getAllProduktType().toArray());
	}

	private class ListListSelectionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent arg0) {
			if (!list.isSelectionEmpty()) {
				if (!list.getValueIsAdjusting()) {
					model.ProduktType pt = (model.ProduktType) list
							.getSelectedValue();
					textField.setText(pt.getNavn());
					textField_1.setText(pt.getBeskrivelse());
					textField_2.setText(pt.getBehandling().getNavn());
				}
			} else {

			}
		}
	}

	private class BtnRedigerProdukttypeActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			m.opretProduktType.setProduktType((ProduktType) list
					.getSelectedValue());
			m.setVisibelity(m.opretProduktType);
		}
	}

	private class BtnOpretProdukttypeActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			m.setVisibelity(m.opretProduktType);
		}
	}

	private class BtnSletProdukttypeActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			service.Service.deleteProduktType((model.ProduktType) list
					.getSelectedValue());
			update();
		}
	}

	@Override
	public void update() {
		list.setListData(service.Service.getAllProduktType().toArray());
	}
}
